package org.spring.user.module.domain.user.service;

import lombok.extern.slf4j.Slf4j;

import org.spring.user.module.domain.user.vo.UserVO;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Service
public class UserService {
    private static final Map<Long, UserVO> userMap = new ConcurrentHashMap<>();

    static {
        UserVO user1 = new UserVO(1L, "小明", 1000);
        UserVO user2 = new UserVO(2L, "小花", 1200);
        userMap.put(user1.getUserId(), user1);
        userMap.put(user2.getUserId(), user2);
    }

    public UserVO queryUserById(Long id) {
        return userMap.get(id);
    }

    public void pay(Long userId, Integer money) {
        UserVO user = userMap.get(userId);
        Integer balance = user.getMoney();
        user.setMoney(balance - money);
        log.info("user: {}, money: {}", user.getUsername(), user.getMoney());
    }

}
